package cn.itsource.pet.service.impl;

import cn.itsource.basic.constant.PethomeConstant;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.utils.CodeGenerateUtils;
import cn.itsource.order.domain.PetAcquisitionOrder;
import cn.itsource.order.mapper.PetAcquisitionOrderMapper;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);
        //获取宠物详情
        PetDetail detail = pet.getDetail();
        //设置宠物
        detail.setPet(pet);
        //保存宠物详情
        petDetailMapper.save(detail);
    }

    @Override
    @Transactional
    public void handleMessage(Pet pet, LoginInfo loginInfo) {

        //设置宠物来源
        pet.setSourceType(PethomeConstant.SEARCH_MASTER);
        //保存宠物
        super.save(pet);
        //保存宠物详情
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.save(detail);
        //把寻主信息状态改为已处理
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearchMasterMsg().getId());
        //已处理
        searchMasterMsg.setState(PethomeConstant.PROCESSED);
        //修改寻主信息状态
        searchMasterMsgMapper.update(searchMasterMsg);

        //根据登录信息查询登录人（员工）
        Employee employee = employeeMapper.loadByLogininfoid(loginInfo.getId());
        //创建收购订单
        PetAcquisitionOrder petAcquisitionOrder = createPetAcquisitionOrder(pet,searchMasterMsg,employee);
        //保存收购订单
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
    }

    /**
     * 创建收购订单
     * @return
     */
    private PetAcquisitionOrder createPetAcquisitionOrder(Pet pet,
                                                          SearchMasterMsg searchMasterMsg,
                                                          Employee employee) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        //摘要
        order.setDigest("[摘要]对"+pet.getName()+"收购订单");
        // 1 待报账(垫付) 2 待打款(银行转账)  3 完成
        order.setState(pet.getCostprice().equals(BigDecimal.ZERO)
                ?PethomeConstant.ACQUISITION_ORDER_COMPLETE:pet.getPaytype());
//        //收购价格
        order.setPrice(pet.getCostprice());
//        //收购地址
        order.setAddress(searchMasterMsg.getAddress());
//        //订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getUser().getId()));
        //如果订单是完成状态，时间就是当前时间，否则完成事件为空
        order.setLastcomfirmtime(order.getState() == PethomeConstant.ACQUISITION_ORDER_COMPLETE ? new Date() : null);
//        //宠物
//        private Pet pet;
        order.setPet(pet);
//        //信息发布者
        order.setUser(pet.getUser());
//        //支付类型  1垫付  2银行转账
//        private Integer paytype;
        order.setPaytype(pet.getPaytype());
//        //收购订单对应的店铺
        order.setShop(pet.getShop());
//        //员工（当前登录人）
        order.setEmployee(employee);

        return order;
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("1");
        System.out.println(BigDecimal.ZERO.equals(bigDecimal));
    }
}
